package com.chaos.eurekaproducer.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.chaos.eurekaproducer.domain.User;
import com.chaos.eurekaproducer.service.IUserService;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.support.ListenerExecutionFailedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryListener;
import org.springframework.retry.backoff.NoBackOffPolicy;
import org.springframework.retry.interceptor.RetryInterceptorBuilder;
import org.springframework.retry.interceptor.RetryOperationsInterceptor;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;

import javax.jws.HandlerChain;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.SynchronousQueue;

/**
 * @author liaopeng
 * @title: RabbitmqConsummer
 * @projectName eureka-producer
 * @description: TODO
 * @date 2021/7/15上午9:46
 */
@Component
public class RabbitMqConsumer {
    private final Log log = LogFactory.getLog(getClass());

    @Autowired
    IUserService userService;

    @RabbitListener(queues = "#{chaosQueue.name}")
    public void process1(Message message, Channel channel, @Headers Map<String, Object> map) throws Exception {
        User user = new User();
        user.setId(1L);
        try {
            String messageId = message.getMessageProperties().getMessageId();
            System.out.println("接收消息id"+messageId+",时间"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
            User userDb = userService.selectById(user);
            if (userDb!=null){
                System.out.println("异步消息userName："+userDb.getName());
            }
            int i=1/0;
        }catch (Exception e){
            throw new Exception("监听消息失败！"+e.getMessage());
        }
    }

    @RabbitListener(queues = "#{deadLetterQueue.name}")
    public void wmsAsyncLabel(Message message) throws Exception {
        try {
            Map<String, Object> headers = message.getMessageProperties().getHeaders();
            String messageId = message.getMessageProperties().getMessageId();
            //历次被投入死信队列的队列信息列表
            ArrayList<HashMap<String, String>> historyQueues = (ArrayList<HashMap<String, String>>) headers.get("x-death");
            //最后一次投入死信队列的队列信息
            HashMap<String, String> lastMap = historyQueues.get(0);
            log.info(JSON.toJSONString("最后一次投入死信队列的队列信息:"+lastMap));
            String exchange = lastMap.get("exchange");
            String routingKey = lastMap.get("routing-keys");
            String queue = lastMap.get("queue");
            log.info("进入死信队列");
        }catch (Exception e){
            log.info("wms异步消息监听失败：" + e.getMessage());
            throw new Exception("wms异步消息监听失败：" + e.getMessage());
        }
    }
}
